package org.andao.security.model;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.andao.core.base.model.TreeSupportModel;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Where;



/**
 * 系统组织架构表. 
 *  
 * @author Marco.hu(hzg139@163.com)
 */
@Entity
@Table(name = "seewo_sys_unit")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class OrgUnit extends TreeSupportModel implements  Serializable{

	private static final long serialVersionUID = -8486029399277663499L;	

		
	@Column(name = "unitName", nullable = false)
	private String unitName;// 组织单位名

	@Column(name = "unitCode", nullable = false, unique = true)
	private String unitCode;// 组织编码

	@Column(name = "unitShortName")
	private String unitShortName;// 组织单位简称

	@Column(name = "unitLevel")
	private int unitLevel = 0;// 组织单位级别

	@ManyToOne(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
	@JoinColumn(name = "PARENTID")	
	private OrgUnit parent;// 父单位

	@Column(name = "ISCHILD")
	private String isChild;// 是否有子集合

	@Column(name = "unitType")
	private String unitType;// 组织类型，如分公司，部门等

	@Column(name = "unitDescript")
	private String unitDescript;// 组织单位描述

	@Column(name = "showorder")
	private int showorder;// 排序号

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "createTime")
	private Date createTime = new Date();// 创建时间
	
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "parent")
	@org.hibernate.annotations.Cascade(value={org.hibernate.annotations.CascadeType.ALL})
	@OrderBy("showorder asc")
	private Set<OrgUnit> childs = new HashSet<OrgUnit>(0);

	@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL }, mappedBy = "orgUnit")
	@OrderBy("showOrder ASC")
	@Where(clause="isDeleted=0")
	private Set<User> user = new LinkedHashSet<User>(0);	//组织用户
	

	@Column(name="PRINCIPAL")
	private String principal;//负责人
	
	@Column(name="LINKMAN")
	private String linkman;//联系人
	
	@Column(name="CONTECTCALL")
	private String contectCall;//联系电话
	
	@Column(name="LOCALCITY")
	private String localCity;//所属城市
	
	@Column(name="ZIPCODE")
	private String zipcode;//邮编
	
	@Column(name="ADDRESS")
	private String address;//地址
	
	@Column(name="STATUS")
	private String status;//状态 字典
	
	//@Column此举是为了@see OrgUnitServiceImpl#getAllChildren(Map<String,Object> map,Object target,String topId,List<Object> resultList)中的getParent()-->getParentId,有待考证
	//@Column(name = "PARENTID", insertable = false, updatable = false) 
	//@author Seewo vick liuguanglin@cvte.cn
	@Transient
	private String parentId;

	@Transient
	private String unitCodeAndName;//编号+名称
	
	
	public String getUnitCodeAndName() {
		return this.unitCode+"-"+this.unitName;
	}

	public void setUnitCodeAndName(String unitCodeAndName) {
		this.unitCodeAndName = unitCodeAndName;
	}

	
	/**
	 * @return the status
	 */
	public String getStatus() {
		return status;
	}

	/**
	 * @param status the status to set
	 */
	public void setStatus(String status) {
		this.status = status;
	}

	

	public Set<OrgUnit> getChilds() {
		return childs;
	}

	public void setChilds(Set<OrgUnit> childs) {
		this.childs = childs;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getContectCall() {
		return contectCall;
	}

	public void setContectCall(String contectCall) {
		this.contectCall = contectCall;
	}

	

	public String getLinkman() {
		return linkman;
	}

	public void setLinkman(String linkman) {
		this.linkman = linkman;
	}

	public String getLocalCity() {
		return localCity;
	}

	public void setLocalCity(String localCity) {
		this.localCity = localCity;
	}

	public String getPrincipal() {
		return principal;
	}

	public void setPrincipal(String principal) {
		this.principal = principal;
	}

	public String getZipcode() {
		return zipcode;
	}

	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	}

	public Set<User> getUser() {
		return user;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public OrgUnit getParent() {
		return parent;
	}

	public void setParent(OrgUnit parent) {
		this.parent = parent;
	}

	public int getShoworder() {
		return showorder;
	}

	public void setShoworder(int showorder) {
		this.showorder = showorder;
	}

	public String getUnitCode() {
		return unitCode;
	}

	public void setUnitCode(String unitCode) {
		this.unitCode = unitCode;
	}

	public String getUnitDescript() {
		return unitDescript;
	}

	public void setUnitDescript(String unitDescript) {
		this.unitDescript = unitDescript;
	}

	public int getUnitLevel() {
		return unitLevel;
	}

	public void setUnitLevel(int unitLevel) {
		this.unitLevel = unitLevel;
	}

	public String getUnitName() {
		return unitName;
	}

	public void setUnitName(String unitName) {
		this.unitName = unitName;
	}

	public String getUnitShortName() {
		return unitShortName;
	}

	public void setUnitShortName(String unitShortName) {
		this.unitShortName = unitShortName;
	}

	public String getUnitType() {
		return unitType;
	}

	public void setUnitType(String unitType) {
		this.unitType = unitType;
	}

	public String getIsChild() {
		return isChild;
	}

	public void setIsChild(String isChild) {
		this.isChild = isChild;
	}

	public String getParentId() {	
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public void setUser(Set<User> user) {
		this.user = user;
	}

	@Override
	public String toString() {
		return unitName;
	}

	@Override
	public String getResourceid() {
		return super.getResourceid();
	}

	@Override
	public String getNodeName() {		
		return getUnitName();
	}

	@Override
	public String getNodeCode() {	
		return getUnitCode();
	}


	@Override
	public String getParentNodeId() {		
		return getParentId();
	}

	@Override
	public Integer getNodeLevel() {		
		return getUnitLevel();
	}	
	
	@Override
	public boolean equals(Object o){ 
		if(!(o instanceof OrgUnit)) {
			return false; 
		}
		OrgUnit org = (OrgUnit)o; 
		return this.getResourceid().equals(org.getResourceid());
	}
}
